<?php
/**
 * Mechanism
 *
 * LICENSE
 *
 * Copyright (c) 2011, Sergey A. Ivonchik <BreatheInMyVoid@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above
 *   copyright notice, this list of conditions and the following disclaimer
 *   in the documentation and/or other materials provided with the
 *   distribution.
 * * Neither the name of the  nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @package Mechanism
 * @license http://www.opensource.org/licenses/bsd-license
 * @author Sergey A. Ivonchik <BreatheInMyVoid@gmail.com>
 * @copyright Copyright (c) 2011 Sergey A. Ivonchik
 */

namespace Mechanism\Router {
	/**
	 * Test for Complex router
	 *
 	 * @package Mechanism\Router
 	 * @author BreatheInMyVoid <BreatheInMyVoid@gmail.com>
	 */
	class ComplexTest extends \PHPUnit_Framework_TestCase {
		/**
		 * Router for tests
		 *
		 * @var Complex $router
		 */
		protected $router = null;

		/**
		 * Routes for tests
		 *
		 * @var array
		 */
		protected $routes = array( );
		
		/**
		 * First uri route for tests
		 * 
		 * @var Uri
		 */
		protected $stdRoute = null;
		
		/**
		 * Second uri route for tests
		 * 
		 * @var Uri
		 */
		protected $unameRoute = null;
		
		/**
		 * Setup method
		 */
		public function setUp( ) {
			$this->stdRoute = new Route\Uri( '[controller]{s}[action]' );
			$this->stdRoute->setDefault( 'controller', 'index' )
						   ->setDefault( 'action', 'index' );
			
			$this->unameRoute = new Route\Uri( '[username]' );
			$this->unameRoute->set( 'controller', 'users' )
							 ->set( 'action', 'show' )
							 ->requireParams( );
			
			$this->routes = array( $this->stdRoute, $this->unameRoute );
			
			try {
				/* Test invalid constructor parameters */
				$this->router = new Complex( array( $this->stdRoute, 'invalid' ) );
			} catch ( Exception\Invalid $e ) {
				$this->router = new Complex( $this->routes );
				return;
			}
			
			$this->fail('An expected Invalid exception has not been raised.');
		}

		/**
		 * Test for constructor
		 * 
		 * @test
		 */
		public function constructor( ) {
			/* Test for empty constructor */
			$router = null;
			
			$router = new Complex( );
			
			$this->assertNotNull( $router );			
		}

		/**
		 * Test for popRoute( )
		 *
		 * @test
		 * @return Complex $router
		 */
		public function popRoute( ) {
			/* Pop takes last pushed and removes route from stack */
			$this->assertSame(
				$this->unameRoute,
				$this->router->popRoute( )
			);

			$this->assertSame(
				$this->stdRoute,
				$this->router->popRoute( )
			);

			/* Returns null if router do not have routes anymore */
			$this->assertNull(
				$this->router->popRoute( )
			);
			
			return $this->router;
		}		
		
		/**
		 * Test for peekRoute( )
		 *
		 * @test
		 * @depends popRoute
		 * @param Complex $router
		 * @return Complex $router
		 */
		public function peekRoute( $router ) {
			/* Pop takes last pushed and  DONT removes route from stack */
			$this->assertSame(
				$this->unameRoute,
				$this->router->peekRoute( )
			);	
			
			/* Return same */
			$this->assertSame(
				$this->unameRoute,
				$this->router->peekRoute( )
			);				
			
			/* Returns null if router do not have routes anymore */
			$this->assertNull(
				$router->popRoute( )
			);								
			
			return $router;
		}		
		
		/**
		 * Test for pushRoute( IRoute $route )
		 *
		 * @test
		 * @depends peekRoute
		 * @param Complex $router
		 * @return Complex $router
		 */
		public function pushRoute( $router ) {
			/* Test for pushRoute and its fluent interface */
			$this->assertSame(
				$router,
				$router->pushRoute( $this->unameRoute )
			);
			
			$this->assertSame(
				$this->unameRoute,
				$router->peekRoute( )
			);
		}
		
		/**
		 * Test for routing( $path )
		 * 
		 * @test
		 */
		public function routing( ) {
			try {
				/* Test with null route */
				$badRoute = new Complex( );
				$badRoute->routing( 'http://www.example.com/' );
			} catch ( Exception\Runtime $e ) {
				/* Remember that routes mathing like stack. See setUp( ) */
				$testRouteA =  'http://www.example.com/';
				$testResultA = array( 'controller' => 'index',
									  'action' => 'index' );
							
				$testRouteB =  'http://www.example.com/news/show/';
				$testResultB = array( 'controller' => 'news',
									  'action' => 'show' );

				$testRouteC =  'http://www.example.com/sergey';
				$testResultC = array( 'username' => 'sergey',
									  'controller' => 'users',
									  'action' => 'show' );				
				
				$testRouteD =  'http://www.example.com/some/wrong/way';
				
				/* Test A */
				$this->assertTrue( 
					$this->router->routing( $testRouteA )
				);
				
				$this->assertEquals(
					$testResultA,
					$this->router->getMatchedRoute( )->get( )
				);

				
				/* Test B */
				$this->assertTrue( 
					$this->router->routing( $testRouteB )
				);
				
				$this->assertEquals(
					$testResultB,
					$this->router->getMatchedRoute( )->get( )
				);				
				
				/* Test C */
				$this->assertTrue( 
					$this->router->routing( $testRouteC )
				);
				
				$this->assertEquals(
					$testResultC,
					$this->router->getMatchedRoute( )->get( )
				);

				/* Test D */
				$this->assertFalse( 
					$this->router->routing( $testRouteD )
				);				
				
				return;
			}
			
			$this->fail('An expected Runtime exception has not been raised.');
		}

		/**
		 * Test for getMatchedRoute( )
		 * 
		 * @test
		 */
		public function getMatchedRoute( ) {
			 /* Cause last test falls (some/wrong/way) getMatchedRoute returns null */	
			$this->assertNull(
				$this->router->getMatchedRoute( )
			);
			
			/* New routing */
			$this->assertTrue( 
				$this->router->routing( 'http://www.example.com/news/politics' )
			);			
			
			/* Test getMatchedRoute */
			$this->assertSame(
				$this->stdRoute,
				$this->router->getMatchedRoute( )
			);
			
			/* Test result */
			$this->assertEquals(
				array( 'controller' => 'news',
					   'action' => 'politics' ),
				$this->router->getMatchedRoute( )->get( )
			);			
		}
	}
}